# -*- coding: utf-8 -*-

# @File    : main.py
# @Date    : 2024-11-22
# @Author  : 郭磊

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from tortoise.contrib.fastapi import register_tortoise

from app import (
    router,
    middleware,
)
from app.initializer import g
from config.settings import ENV

app = FastAPI()

g.setup()
g.logger.info(f"使用配置文件 '.env.{ENV}'")

# fastapi运行时自动创建数据库表
# register_tortoise(
#     app=app,
#     config=g.conf.conf_from_yaml("MYSQL_CONFIG"),
#     generate_schemas=True,  # 如果数据库为空，则自动生成对应表单，生产环境不开
#     add_exception_handlers=True,  # 是否添加异常处理，生产环境不开
# )

# 静态文件
app.mount("/static", StaticFiles(directory="static"), name="static")

# #
router.register_default_router(app)
router.register_routers_dynamically(app, api_version='v1')
middleware.register_middlewares(app)
